﻿Imports System.Data.SqlClient


Public Class GuestView
    Private Sub LoadData()
        cmd.Parameters("@S") = "%" & textsearch.Text & "%"
        Me.dgv.DataSource = cmd.ExecuteDataTable("SELECT * FROM Guest WHERE GuestName LIKE @S AND Active=1;")
    End Sub

    Private Sub textsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textsearch.TextChanged
        LoadData()
    End Sub

    Private Sub formGuestview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.dgv)
        LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New GuestAdd
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            LoadData()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        'If MessageBox.Show(My.Resources.AreYouSureWantToDelete, My.Resources.Delete, MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
        '    Exit Sub
        'End If
        Dim row = Me.dgv.SelectedRows(0)
        Dim id = row.Cells("GuestId").Value


        cmd.Parameters("@GuestId") = id
        cmd.ExecuteNonQuery("UPDATE Guest SET Active=0 WHERE GuestId=@GuestId")

        LoadData()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row = Me.dgv.SelectedRows(0)
        Dim frm As New GuestAdd()
        frm.TextBox5.Text = row.Cells("GuestId").Value
        frm.TextBox1.Text = row.Cells("GuestNo").Value
        frm.TextBox2.Text = row.Cells("GuestName").Value
        frm.DateTimePicker1.Value = row.Cells("GuestDob").Value
        frm.TextBox3.Text = row.Cells("GuestAddress").Value
        frm.TextBox4.Text = row.Cells("GuestPhone").Value
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            LoadData()
        End If



    End Sub
End Class